package com.example.cstr.controller;

import com.example.cstr.entity.File;
import com.example.cstr.entity.Message;
import com.example.cstr.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.MessageFormat;

@RestController
@RequestMapping("/api/Files")
@Api(tags = "文件模块")
public class FilesController {
    @Resource
    private FileService fileService;

    @ApiOperation(value = "添加文件信息")
    @PostMapping("/add")
    public Message insertFile(File file){
        return fileService.insertSelective(file)>0?Message.success(null).add(file.getFileId()): Message.fail(null);
    }

    @ApiOperation(value = "删除文件信息")
    @DeleteMapping("/delete")
    public Message deleteFile(Integer fileId){
        return fileService.deleteByPrimaryKey(fileId)>0?Message.success(null):Message.fail(null);
    }

    @ApiOperation(value = "修改文件信息")
    @PutMapping("/update")
    public Message updateFile(File file){
        return fileService.updateByPrimaryKeySelective(file)>0?Message.success(null):Message.fail(null);
    }

    @ApiOperation(value = "根据文件id查询文件信息")
    @GetMapping("/selectByFileId")
    public Message selectByFileId(Integer fileId){
        return fileService.selectByPrimaryKey(fileId)!=null?Message.success(null).add(fileService.selectByPrimaryKey(fileId)):Message.fail(null);
    }

    @ApiOperation(value = "查询所有文件信息")
    @GetMapping("/selectAllFileInfo")
    public Message selectAllFileInfo(){
        return Message.success(null).add(fileService.selectAllFile());
    }

    @ApiOperation(value = "分页查询-查询所有文件信息")
    @GetMapping("/pagingSearch")
    public Message PagingSearch(@RequestParam Integer pageNum,@RequestParam Integer size){
        return fileService.PagingSearch(pageNum,size);
    }

    @ApiOperation(value = "通过学习内容id查询文件信息")
    @GetMapping("/selectFileInfoByscId")
    public Message selectFileInfoByscId(Integer scId){
        return fileService.selectFileInfoByscId(scId)!=null?Message.success(null).add(fileService.selectFileInfoByscId(scId)):Message.fail(null);
    }
}
